﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace RushTracker.Core
{
    public sealed class Site
    {
        private XmlDocument _configDoc;
        private XDocument _xDoc;
        private bool _isInitialized = false;

        Site()
        {
            this._configDoc = new XmlDocument();
        }

        public static Site Instance
        {
            get { return Nested.instance; }
        }

        public string AnnounceUrl
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["announceUrl"].Value; }
        }

        public string SQLiteDatabasePath
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["sqliteDbPath"].Value; }
        }

        public string DefaultLanguage
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["defaultLanguage"].Value; }
        }

        public string LanguageFileExtension
        {
            get { return this._configDoc.SelectSingleNode("configuration/site").Attributes["languageFileExtension"].Value; }
        }

        public string LanguageFolder
        {
            get { return this._configDoc.SelectSingleNode("configuration/site").Attributes["languageFolder"].Value; }
        }

        public string Name
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["name"].Value; }
        }

        public string Title
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["title"].Value; }
        }

        public TrackerMode TrackerMode
        {
            get { return (TrackerMode)Convert.ToInt32(this._configDoc.SelectSingleNode("/configuration/site").Attributes["trackerMode"].Value); }
        }

        public string UploadPath
        {
            get { return this._configDoc.SelectSingleNode("/configuration/site").Attributes["uploadPath"].Value; }
        }

        public void Initialize(string configPath)
        {
            if (this._isInitialized)
                return;

            this._configDoc.Load(configPath);
            this._xDoc = XDocument.Load(configPath);
            
            this._isInitialized = true;
        }

        public string ResolveUrl(string relativePath)
        {
            return HttpContext.Current.Server.MapPath(relativePath);
        }

        class Nested
        {
            static Nested() { }

            internal static readonly Site instance = new Site();
        }
    }
}
